#!/bin/bash

# 获取当前脚本所在的目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# 定义 wondertrader 的根目录（假设 wondertrader 在 demos 的同级目录）
WONDERTRADER_DIR="$(dirname "$SCRIPT_DIR")/wondertrader"

# 定义 demos 的根目录（当前脚本所在的目录）
DEMOS_DIR="$SCRIPT_DIR"

# 打印 wondertrader 和 demos 的目录
echo "Wondertrader 目录: $WONDERTRADER_DIR"
echo "Demos 目录: $DEMOS_DIR"

# 检查目录是否存在
if [ ! -d "$WONDERTRADER_DIR" ]; then
    echo "错误: wondertrader 目录不存在: $WONDERTRADER_DIR"
    exit 1
fi

if [ ! -d "$DEMOS_DIR" ]; then
    echo "错误: demos 目录不存在: $DEMOS_DIR"
    exit 1
fi

# 定义一个函数来执行 cp 操作并检查结果
copy_file() {
    SOURCE_FILE="$1"
    DEST_FILE="$2"

    echo "正在复制文件: $SOURCE_FILE -> $DEST_FILE"
    cp "$SOURCE_FILE" "$DEST_FILE"

    # 检查 cp 操作是否成功
    if [ $? -eq 0 ]; then
        echo "复制成功: $SOURCE_FILE -> $DEST_FILE"
    else
        echo "错误: 复制失败: $SOURCE_FILE -> $DEST_FILE"
        exit 1
    fi
}

# 检查并解压 QuoteFactory_ubuntu.zip
if [ ! -d "$DEMOS_DIR/QuoteFactory_ubuntu" ]; then
    echo "QuoteFactory_ubuntu 文件夹不存在，正在解压 QuoteFactory_ubuntu.zip"
    unzip "$DEMOS_DIR/QuoteFactory_ubuntu.zip" -d "$DEMOS_DIR"
    if [ $? -eq 0 ]; then
        echo "解压成功: QuoteFactory_ubuntu.zip"
    else
        echo "错误: 解压失败: QuoteFactory_ubuntu.zip"
        exit 1
    fi
else
    echo "QuoteFactory_ubuntu 文件夹已存在，跳过解压"
fi

# 检查并解压 loadRunner_ubuntu.zip
if [ ! -d "$DEMOS_DIR/loadRunner_ubuntu" ]; then
    echo "loadRunner_ubuntu 文件夹不存在，正在解压 loadRunner_ubuntu.zip"
    unzip "$DEMOS_DIR/loadRunner_ubuntu.zip" -d "$DEMOS_DIR"
    if [ $? -eq 0 ]; then
        echo "解压成功: loadRunner_ubuntu.zip"
    else
        echo "错误: 解压失败: loadRunner_ubuntu.zip"
        exit 1
    fi
else
    echo "loadRunner_ubuntu 文件夹已存在，跳过解压"
fi

# 更新 loadRunner_ubuntu
copy_file "$WONDERTRADER_DIR/src/build_all/build_x64/Release/bin/Loader/libCTPLoader.so" "$DEMOS_DIR/loadRunner_ubuntu"
copy_file "$WONDERTRADER_DIR/src/build_all/build_x64/Release/bin/Loader/libCTPOptLoader.so" "$DEMOS_DIR/loadRunner_ubuntu"
copy_file "$WONDERTRADER_DIR/src/build_all/build_x64/Release/bin/Loader/LoaderRunner" "$DEMOS_DIR/loadRunner_ubuntu"
copy_file "$WONDERTRADER_DIR/src/build_all/build_x64/Release/bin/libTraderCTP.so" "$DEMOS_DIR/loadRunner_ubuntu/traders"

# 更新 QuoteFactory_ubuntu
copy_file "$WONDERTRADER_DIR/src/build_all/build_x64/Release/bin/QuoteFactory/QuoteFactory" "$DEMOS_DIR/QuoteFactory_ubuntu"
copy_file "$WONDERTRADER_DIR/src/build_all/build_x64/Release/bin/QuoteFactory/libWtDataStorage.so" "$DEMOS_DIR/QuoteFactory_ubuntu"
copy_file "$WONDERTRADER_DIR/src/build_all/build_x64/Release/bin/QuoteFactory/parsers/libParserCTP.so" "$DEMOS_DIR/QuoteFactory_ubuntu/parsers"
copy_file "$WONDERTRADER_DIR/src/build_all/build_x64/Release/bin/QuoteFactory/parsers/libParserCTPMini.so" "$DEMOS_DIR/QuoteFactory_ubuntu/parsers"
copy_file "$WONDERTRADER_DIR/src/build_all/build_x64/Release/bin/QuoteFactory/parsers/libParserFemas.so" "$DEMOS_DIR/QuoteFactory_ubuntu/parsers"
copy_file "$WONDERTRADER_DIR/src/build_all/build_x64/Release/bin/QuoteFactory/parsers/libParserXTP.so" "$DEMOS_DIR/QuoteFactory_ubuntu/parsers"

echo "所有文件复制完成！"